//
//  FBYHomeService.m
//  CheckmeRing
//
//  Created by 董鹏 on 2022/3/26.
//

#import "FBYHomeService.h"

#import <AFNetworking/AFNetworking.h>

@implementation FBYHomeService

- (void)searchMessage:(NSString *)pageNum andWithDic:(id)Alldic andUrl:(NSString *)url andSuccess:(void (^)(NSString *))success andFailure:(void (^)(int))failure{
    
    //1.创建ADHTTPSESSIONMANGER对象
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    
    //2.设置该对象返回类型
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"charset=utf-8", nil];

    
    if ([pageNum isEqualToString:[NSString stringWithFormat:@"get"]]) {
        
        NSString *urlstr= [NSString stringWithFormat:@"%@", url];
        
        NSLog(@"%@",urlstr);
        //调出请求头
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        //将token封装入请求头
        
        NSUserDefaults *tokenid = [NSUserDefaults standardUserDefaults];
        NSString *token = [tokenid objectForKey:@"tokenid"];
        NSLog(@"%@",token);
        
        [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
        
        [manager GET:urlstr parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSString * str  =[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            NSLog(@"%@", str);
            success(str);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
        
    }else {
        NSString *urlstr= [NSString stringWithFormat:@"%@", url];
        NSLog(@"%@",urlstr);
        [manager.requestSerializer setValue:@"1" forHTTPHeaderField:@"x-signature"];
        [manager.requestSerializer setValue:@"2" forHTTPHeaderField:@"x-timestamp"];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        
        [manager POST:urlstr parameters:Alldic headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSString * str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            NSLog(@"%@", str);
            success(str);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            NSLog(@"%@", error);
        }];
        
    }
}

@end
